#-*- coding: utf-8 -*-

"""
    Copyright 2010 Kacper Żuk <kacper.b.zuk@gmail.com>
    
    This file is part of Urh Dungeon 2.

    Urh Dungeon 2 is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Urh Dungeon 2 is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Urh Dungeon 2.  If not, see <http://www.gnu.org/licenses/>.
"""

import vars, sys, cPickle, pygame
from lang import *
lang = langs[vars.language]
images = dict()

def fitInRect(text, font, rect):
    """ Inspired by http://www.pygame.org/pcr/text_rect/index.php """
    
    words = text.split(" ")
    done = False
    while not done:
        new = list()
        done = True
        for word in words:
            if font.size(word)[0] > rect.w:
                done = False
                y = 0
                while True:
                    y += 1
                    if font.size(word[:-y])[0] < rect.w:
                        new.append(word[:-y])
                        new.append(word[-y:])
                        break
            else:
                new.append(word)
        words = new
            
    lines = list()
    line = ""
    for word in words:
        tmp = line + word + " "
        if font.size(tmp)[0] < rect.w:
            line = tmp
        else:
            lines.append(line)
            line = word + " "
    lines.append(line)
    return lines
        
def printSurface(surface, text, size, color, coor, center=False, bg=None):
    font = pygame.font.Font(vars.font, size)
    texts = font.render(text, 1, color)
    if not center:
        textpos = coor
    else:
        textpos = texts.get_rect(center=coor)
    if bg:
        tmp = pygame.Surface((texts.get_width(),texts.get_height()))
        tmp.fill(bg)
        tmp.blit(texts, (0,0))
        texts = tmp
    surface.blit(texts, textpos)
    return texts.get_width()
    
def printConsole(text, color):
    vars.panelupdate = True
    rect = pygame.Rect(720, 500, 130, 193)
    font = pygame.font.Font(vars.font, 10)
    line = fitInRect(text, font, rect)
    new = pygame.Surface((130, 193), pygame.SRCALPHA)
    new.blit(vars.consoleimg, (0, 0-font.get_height()*len(line)))
    x = 0
    for l in line:
        printSurface(new, l, 10, color, (0, rect.h + font.get_height()*x - len(line)*font.get_height() ))
        x += 1
    vars.consoleimg = new
    
def img(name):
    global images, lang
    if name == 'black':
        black = pygame.Surface((32,32))
        black.fill((0,0,0))
        return black
    try:
        return images[name]
    except:
        try:
            if name == "icon":
                images[name] = pygame.image.load(vars.images[name])
            else:
                images[name] = pygame.image.load(vars.images[name]).convert_alpha()
            return images[name]
        except pygame.error:
            try:
                lang[vars.language]
            except KeyError:
                lang = langs[vars.language]
            printConsole(lang['noFile']+vars.images[name], (255, 0, 0))
            return pygame.Surface((32,32))
